package com.myway.dto.tcscenery.support;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;


public class DTOPageList<T> {
   
	private Integer page;
	
	private Integer pagesize;
	
	private Integer totalPage;
	
	private Integer totalCount;
	
	private String imgbaseURL;
	
	private List<T> tlist;

	
	public String getImgbaseURL() {
		return imgbaseURL;
	}

	public void setImgbaseURL(String imgbaseURL) {
		this.imgbaseURL = imgbaseURL;
	}

	public Integer getPage() {
		return page;
	}

	public void setPage(Integer page) {
		this.page = page;
	}

	public Integer getPagesize() {
		return pagesize;
	}

	public void setPagesize(Integer pagesize) {
		this.pagesize = pagesize;
	}

	public Integer getTotalPage() {
		return totalPage;
	}

	public void setTotalPage(Integer totalPage) {
		this.totalPage = totalPage;
	}

	public Integer getTotalCount() {
		return totalCount;
	}

	public void setTotalCount(Integer totalCount) {
		this.totalCount = totalCount;
	}

	public List<T> getTlist() {
		return tlist;
	}

	public void setTlist(List<T> tlist) {
		this.tlist = tlist;
	}
	
	public DTOPageList<T> xmlToList(String xml){
		 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
	     DocumentBuilder db = null;
	     Document doc = null;
	     try {
			db = dbf.newDocumentBuilder();
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	 	 try {
			doc = db.parse(new ByteArrayInputStream(xml.getBytes("UTF-8")));
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	     NodeList node = doc.getElementsByTagName("sceneryList");
	     DTOPageList<T> list = new DTOPageList<T>();
		for(int i = 0; i < node.getLength(); i++){
			 Element element = (Element)node.item(i);
			 Integer page = Integer.parseInt(element.getAttribute("page").toString());
			 list.setPage(page);
			 Integer pageSize = Integer.parseInt(element.getAttribute("pageSize").toString());
			 list.setPagesize(pageSize);
			 Integer totalPage = Integer.parseInt(element.getAttribute("totalPage").toString());
			 list.setTotalPage(totalPage);
			 Integer totalCount = Integer.parseInt(element.getAttribute("totalCount").toString());
			 list.setTotalCount(totalCount);
			 String  imgbaseURL = element.getAttribute("imgbaseURL").toString();
			 list.setImgbaseURL(imgbaseURL);
		}

		return list;
	}
	
}
